package indi.zqc.mqtt.producer;

import com.alibaba.fastjson.JSONObject;
import indi.zqc.mqtt.producer.handler.MqttMessageGateway;
import indi.zqc.mqtt.producer.pojo.ApiData;
import indi.zqc.mqtt.producer.pojo.MqttObject;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * mqtt生产端
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context =
                new SpringApplicationBuilder(Application.class)
                        .web(false)
                        .run(args);
        MqttObject mqttObject = new MqttObject();
        List<String> list = new LinkedList<>();
        mqttObject.setId("11");
        list.add("222");
        list.add("333");
        mqttObject.setIds(list);
        try {
            ByteArrayOutputStream byteArrayOutputStream= new ByteArrayOutputStream();
            ObjectOutputStream o = new ObjectOutputStream(byteArrayOutputStream);
            o.writeObject(mqttObject);
            //发送的消息
            Map<Character,Integer> has = new LinkedHashMap<>();
            String a = "a";
            System.out.println(0xff);
//            has.put(a.charAt(1),1);
//            has.entrySet().iterator().next().getKey();
            byte[] bytes = getInfo();
            ApiData apiData = new ApiData();
            apiData.setTime(System.currentTimeMillis());
            apiData.setType("SOC");
            apiData.setMsgId("0");
            apiData.setData(encodeBase64("{'sn1':'1' ,'model1':'2','soc1':'3','sn2':'sn2','model2':'2','soc2':'2'}"));
            String jsonStr = JSONObject.toJSONString(apiData);
            MqttMessageGateway gateway = context.getBean(MqttMessageGateway.class);
            gateway.sendMessage2Mqtt(jsonStr,"wootop/r1/wifi");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private final static Base64.Decoder decoder = Base64.getDecoder();

    private final static Base64.Encoder encoder = Base64.getEncoder();
    public static String encodeBase64(String text) {
        String result = null;
        try {
            result = encoder.encodeToString(text.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }


    public byte[] Int2Bytes_LE(int iValue){
        byte[] buf = new byte[4];
        // 先写int的最后一个字节
        buf[0] = (byte)(iValue & 0xFF);
        // int 倒数第二个字节
        buf[1] = (byte)((iValue & 0xFF00) >> 8 );
        // int 倒数第三个字节
        buf[2] = (byte)((iValue & 0xFF0000) >> 16 );
        // int 第一个字节
        buf[3] = (byte)((iValue & 0xFF000000) >> 24 );
        return buf;
    }

    private static byte[] getInfo(){
        int length = 0x69;
        byte[] streamHeader = {0x5A,0x6B
                ,0x01
                ,0x57, 0x50, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34
                , 0x00 ,0x00, 0x00 , (byte)0xf7
                , 0x00 ,(byte) 0x77};

        List<Byte> list = new LinkedList<>();
        for(int i=0; i<streamHeader.length; i++){
            list.add(streamHeader[i]);
        }

        //随机填充
        for (int i=0; i<length; i++){
            Random rand = new Random();
            list.add(intToByteArray(rand.nextInt(255 - 0 + 1) + 0));
        }
        //包尾
        list.add((byte)0x7c);
        list.add((byte)0x8d);

        byte[] data = new byte[list.size()];

        //模拟数据流
        for(int i=0; i<list.size(); i++){
            data[i] = list.get(i);
        }
        byte[] byteHeader = new byte[5];
        System.arraycopy(data, 0, byteHeader, 0, 5);
        //对比包头
        if (Arrays.equals(byteHeader, new byte[]{0x5A, 0x6B, 0x01, 0x57, 0x50})){
            System.out.println("Ok");
            //TODO
        }
        System.out.println("");
        return data;
    }

    /**
     * int到byte[] 由高位到低位
     * @param i 需要转换为byte数组的整行值。
     * @return byte数组
     */
    public static byte intToByteArray(int i) {
        if (i<256){
            return  (byte)i;
        } else {
            return 0x0;
        }
    }
}
